package com.example.itshome.network;

import android.app.Activity;
import com.example.itshome.entity.UserEntity;
import com.example.itshome.manager.UserInfoManager;
import com.example.itshome.util.ToastUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class ChangeNicknameRequest extends AbstractNetworkRequest{
    private String nickname;

    public ChangeNicknameRequest setNickname(String nickname) {
        this.nickname = nickname;
        return this;
    }

    public ChangeNicknameRequest(Activity activity) {
        super(activity);
    }

    @Override
    protected String getPathAndParams() {
        return "user/change-nickname?username="+ UserInfoManager.getInstance().getUser().getUsername()+
                "&nickname="+nickname;
    }

    @Override
    protected void handleResult(String result) {
        try {
            JSONObject jsonObject=new JSONObject(result);
            int code=jsonObject.getInt("code");
            if(code!=200){
                return;
            }
            setHandleResultRunnable(new Runnable() {
                @Override
                public void run() {
                    ToastUtil.showToast("修改成功",activity);
                    UserInfoManager.getInstance().getUser().setNickname(nickname);
                    UserInfoManager.getInstance().callRunner();
                    activity.finish();
                }
            });
        }catch (JSONException e){
            e.printStackTrace();
        }

    }
}
